package com.code.leetcode.before50;

import java.util.HashMap;
import java.util.Stack;

/**
 * @author codeDorado
 * @version 1.0
 * @date 2020/7/11 20:06
 */
public class The20 {
    HashMap<Character, Character> map;
    public The20() {
        map = new HashMap<>();
        map.put(')', '(');
        map.put(']', '[');
        map.put('}', '{');
    }

    public boolean isValid(String s) {
        if (s.length() == 0) return true;
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (map.containsKey(c)) {
                if (stack.isEmpty() || !stack.pop().equals(map.get(c))) {
                    return false;
                }
            } else {
                stack.push(c);
            }
        }
        return stack.empty();
    }
}
